package uk.co.richardwkirk.parkrun.android.events.distance;

import java.util.ArrayList;

import uk.co.richardwkirk.parkrun.data.athletes.AthleteEventSummary;
import uk.co.richardwkirk.parkrun.data.athletes.AthleteSummary;
import uk.co.richardwkirk.parkrun.data.events.Event;
import android.location.Location;

public class EventWithDistance {

	private static final double UNKNOWN_DISTANCE = -99;
	private Event event;
	private double distance;
	private ArrayList<String> details = new ArrayList<String>();
	private boolean visited = false;
	
	public static EventWithDistance create(Event e, Location lastLocation, AthleteSummary athlete) {
		EventWithDistance eventWithDistance = new EventWithDistance();
		eventWithDistance.setEvent(e);
	
		float[] results = new float[2];
		if (lastLocation != null) {
			Location.distanceBetween(lastLocation.getLatitude(), lastLocation.getLongitude(), e.getLocation().getLatitude(), e.getLocation().getLongitude(), results);
			eventWithDistance.distance = results[0];
		}
		else {
			eventWithDistance.distance = UNKNOWN_DISTANCE;
		}
		
		eventWithDistance.details.add("Show event page.");
		eventWithDistance.details.add("Region: " + e.getRegionString());
		eventWithDistance.details.add("Event is " + eventWithDistance.getDistanceString() + " away in a straight line.");
		eventWithDistance.details.add("Show event on map.");
		eventWithDistance.details.add("Navigate to event.");
		if (athlete != null && athlete.hasVisited(e.getShortName())) {
			eventWithDistance.visited = true;
			AthleteEventSummary eventSummary = athlete.getEventSummary(e.getShortName());
			eventWithDistance.details.add("Visited " + eventSummary.getRunCount() + " times with best time " + eventSummary.getBestTime() + ".");
		}
		
		return eventWithDistance;
	}

	public String getDistanceString() {
		if (distance != UNKNOWN_DISTANCE) {
			int hundredMDistance = (int) (distance / 100);
			double kmDistance =  hundredMDistance / 10.0;
			return String.valueOf(kmDistance) + "km";
		}
		else {
			return "Unknown Distance";
		}
	}

	public ArrayList<String> getDetails() {
		return details;
	}

	public String getEventName() {
		return getEvent().getFullName();
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public double getDistance() {
		return distance;
	}
	
	public boolean isVisited() {
		return visited;
	}
	
	
}
